package com.rs.yyds.controller;

import com.rs.yyds.bean.User;
import com.rs.yyds.bean.UserVo;
import com.rs.yyds.common.BaseController;
import com.rs.yyds.service.UserService;
import com.rs.yyds.util.AjaxResult;
import com.rs.yyds.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/userController")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 用户添加
     *  *@RequestBody要求前端页面传值参数格式为json
     * @param user
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST )
    public AjaxResult add(User user, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Map<String,Object> map =userService.addUser(user);
        int code = (int)map.get("code");
        if(code!=0){
            return AjaxResult.failure();
        }
        return AjaxResult.success();
    }

    /**
     * 修改
     * @param user
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST )
    public AjaxResult edit(UserVo user, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
//        判断密码是否被更改，userPassword为数据库中读取的旧密码（隐藏），newUserPassword为展示的密码
        if(!user.getNewUserPassword().equals(user.getUserPassword())){
            user.setUserPassword(MD5Util.getMD5Code(user.getNewUserPassword()));
        }
        int code =userService.updateUserById(user);
        if(code<=0){
            return AjaxResult.failure();
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST )
    public AjaxResult delete(int[] ids){
//        根据用户id进行删除操作
        int i = userService.deleteUsers(ids);
        if(i<=0){
            return AjaxResult.failure();
        }
        return AjaxResult.success();
    }

    /**
     * 分页查询
     * sql语句必须要进行order by排序，避免出现重复数据
     * @param user
     * @return
     */
    @RequestMapping(value = "/listForPage")
    @ResponseBody
    public AjaxResult listForPage(User user) {
        List<User> list = userService.findUser(user);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success();
        }
        return getResult(user,list,list.size());
    }

}
